package idv.arthur.work;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

/**
 * Servlet implementation class MappingEdit
 */
//@WebServlet(description = "該tcID下的所有uid", urlPatterns = { "/MappingEdit" })
public class MappingEdit extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public MappingEdit() {
        super();
    }

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String tcID = request.getParameter("q_tcID");				//測試案例ID
		
		
		String strSQL = "SELECT t3.nickName,t2.uid,t1.numKeyPoint,t1.tcTestData,t2.active,t2.tcID,t1.tcName,t1.tcKeyPoint,t1.tcProcess,t1.tcResExp,t1.CreatedDateTime,t1.Creator FROM tms_testcase t1, tms_tcidmapping t2,tms_personnel t3 WHERE t1.Creator=t3.userID AND t1.uid=t2.uid AND t2.tcID='" + tcID + "'";
		
		Widgets wid = new Widgets();
		Connection conn = wid.getConn(true);
		PreparedStatement ps = null;
		ResultSet rs = null;
		ArrayList<TestCase> testCases = new ArrayList<TestCase>();
		String tt = "";
		
		try {
			ps = conn.prepareStatement(strSQL);
			rs = ps.executeQuery();
			while (rs.next()) {
				TestCase testCase = new TestCase();
				testCase.setNumKeyPoint(rs.getInt("numKeyPoint"));
				testCase.setTcTestData(rs.getString("tcTestData"));
				testCase.setTcID(rs.getString("tcID"));
				testCase.setuID(rs.getInt("uID"));
				testCase.setActivated(rs.getBoolean("active"));
				testCase.setTcName(rs.getString("tcName"));
				testCase.setTcKeyPoint(rs.getString("tcKeyPoint"));
				testCase.setTcProcess(rs.getString("tcProcess"));
				testCase.setTcResExp(rs.getString("tcResExp"));
				testCase.setCreatedDateTime(rs.getDate("CreatedDateTime").toString());
				testCase.setCreator(rs.getString("nickName")+"("+rs.getString("Creator")+")");
				testCases.add(testCase);
			}
			if (testCases.size() > 0) {
				tt = new Gson().toJson(testCases);
				tt = tt.replace("[", "{\"mappingData\":[")+ ",\"strSQL\":[{\"strSQL\":\""+strSQL+"\"}]}";
			} else {
				tt = "No Result==>size is:"+testCases.size();
			}
		} catch (SQLException e) {
			wid.keepErrorMes(conn, ps, e,"", true);
		} finally {
			try {
				if (rs!= null) {rs.close();}
				if (ps!= null) {ps.close();}
				if (conn!= null) {conn.close();}
			} catch (SQLException e) {}
		}
		
		response.setContentType("application/json");
		response.setCharacterEncoding("UTF-8");
		response.getWriter().write(tt);
	}	
}
